package com.sinothk.droid.chart.barChartHorizontal;

import android.app.Activity;
import android.graphics.Color;
import android.util.DisplayMetrics;

import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class BarChartHorizontalManager {

    private Activity mActivity = null;
    private HorizontalBarChart hBarChart = null;
    private BarChartParam barChartParam = null;
    private String[] xSet = null;
    private float[] ySet = null;

    private BarChartHorizontalManager instant = null;

    public BarChartHorizontalManager(Activity mActivity, HorizontalBarChart hBarChart) {
        this.mActivity = mActivity;
        this.hBarChart = hBarChart;
        instant = this;
    }

    public BarChartHorizontalManager setParam(BarChartParam barChartParam) {
        this.barChartParam = barChartParam;
        return instant;
    }

    public BarChartHorizontalManager setData(String[] xSet, float[] ySet) {
        this.xSet = xSet;
        this.ySet = ySet;
        return instant;
    }

    public void show() {
        setDes();
        setXAxis(xSet);
        setYAxis(ySet);
        loadData(ySet);
    }

    private void setDes() {
        Description description = new Description();
        description.setText("统计图");
        description.setTextSize(20f);
        description.setTextColor(Color.BLACK);

        //获取屏幕的中间坐标

        //获取屏幕的中间坐标
        DisplayMetrics dm = new DisplayMetrics();
//        this.getSystemService(Context.WINDOW_SERVICE).defaultDisplay.getMetrics(dm)

        float x = (dm.widthPixels / 2f);

        description.setPosition(x, 40f);
        hBarChart.setDescription(description);
        // 设置不能触摸
        hBarChart.setTouchEnabled(false);
    }

    private void setXAxis(String[] xSet) {
        XAxis xAxis = hBarChart.getXAxis();
        xAxis.setTextSize(14f);

        int textColor = Color.GRAY;
        if (barChartParam != null && barChartParam.getXTextColor() != 0) {
            textColor = barChartParam.getXTextColor();
        }
        xAxis.setTextColor(textColor);
        //把最小值设置为负数能够为下方留出点空白距离
        xAxis.setAxisMinimum(-0.6f);
        xAxis.setDrawGridLines(false);
        //将x轴显示在左侧
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置x轴的偏移量
        xAxis.setXOffset(6f);

        //自定义样式
        xAxis.setLabelCount(xSet.length, false);
        xAxis.setAxisMaximum((xSet.length - 1) + 0.6f);

//        val xSet = arrayOf("特惠大床房", "双床房", "一等房", "总统套房", "特色房")//
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return xSet[(int) value]; //ss
            }
        });
    }

    private void setYAxis(float[] ySet) {
        //两边的y轴都要设置y轴的最小值才能在柱状图上面显示数值
        //不然是看不到效果的
        YAxis tepAxis = hBarChart.getAxisLeft();
        tepAxis.setAxisMaximum(getSetMax(ySet));
        tepAxis.setAxisMinimum(0f);
        tepAxis.setEnabled(false);

        YAxis yAxis = hBarChart.getAxisRight();
        yAxis.setTextSize(12f);
        yAxis.setAxisMinimum(0f);
        yAxis.setAxisMaximum(80f);

        //自定义样式
        yAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
//                return value.toString() + "0%"
                return "";
            }
        });
    }

    private float getSetMax(float[] ySet) {
        float maxVal = 0f;
        for (float value : ySet) {
            if (maxVal < value) {
                maxVal = value;
            }
        }
        return maxVal + (maxVal / 5) * 1f;
    }

    private void loadData(float[] ySet) {

        //不绘制图例
        hBarChart.getLegend().setEnabled(false);
        //自动对齐
        hBarChart.setFitBars(true);
        hBarChart.setExtraOffsets(20f, 50f, 30f, 30f);
        //将文本绘制在柱块上还是柱块里面
        hBarChart.setDrawValueAboveBar(true);

        //从下往上绘制
        ArrayList<BarEntry> entries = new ArrayList<>();
//        val ySet = floatArrayOf(50f, 20f, 30f, 40f, 50f)//

        for (int i = 0; i < ySet.length; i++) {
            entries.add(new BarEntry(i * 1f, ySet[i]));
        }

        BarDataSet barDataSet = new BarDataSet(entries, "");
        //柱块上的字体大小
        barDataSet.setValueTextSize(14f);
        barDataSet.setBarBorderWidth(0.2f);
        //柱块上的字体颜色
        int valueTextColor = Color.BLUE;
        if (barChartParam != null && barChartParam.getTopTextColor() != 0) {
            valueTextColor = barChartParam.getTopTextColor();
        } else {

        }
        barDataSet.setValueTextColor(valueTextColor);
//        barDataSet.valueTypeface = Typeface.DEFAULT_BOLD //加粗

        //自自定义样式
        barDataSet.setValueFormatter(new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return String.format("%.2f", value) + "%"; //保留两位小数
            }
        });

        //依次设置每次柱块的颜色 barColormActivity!!.resources.getColor(R.color.purple_700)
        List<Integer> colors = new ArrayList<>();
        colors.add(Color.BLUE);

        if (barChartParam != null && barChartParam.getBarColors() != null) {
            colors.clear();
            colors = barChartParam.getBarColors();
        }
        barDataSet.setColors(colors);

        BarData barData = new BarData(barDataSet);
        //设置柱块的宽度
        barData.setBarWidth(0.5f);
        hBarChart.setData(barData);
    }
}
